﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Libraries.Common;
using Libraries.Entities;
using Libraries.Repositories;

namespace Libraries.Repositories
{
    public class CourseInstructorRepository
    {
        #region Instructor

        private CourseInstructor ParseToEntityCourseInstructor(IDataRecord dataReader) {
            if (dataReader != null) {
                if (dataReader.FieldCount == 8) {
                    return new CourseInstructor() {
                                                      CourseId = dataReader["CourseId"].ToInt32OrDefault(),
                                                      Date = dataReader["Date"].ToDateTimeOrDefault(),
                                                      Duration = dataReader["Duration"].ToInt32OrDefault(),
                                                      FinalPoint = dataReader["FinalPoint"].ToInt32OrDefault(),
                                                      Instructor = dataReader["FullName"].ToStringOrDefault(),
                                                      Point = dataReader["Point"].ToInt32OrDefault(),
                                                      TopicName = dataReader["TopicName"].ToStringOrDefault(),
                                                      TypeOfTrainingContribution = dataReader["TypeContribution"].ToStringOrDefault()
                                                  };
                } else {
                    var result = new CourseInstructor();

                    result.CourseInstructorId = (int)dataReader["CourseInstructorId"];
                    result.CourseId = (int)dataReader["CourseId"];
                    result.CourseName = (string)dataReader["CourseName"];
                    result.TopicId = (int)dataReader["TopicId"];
                    result.TopicName = (string)dataReader["TopicName"];
                    result.InstructorId = (string)dataReader["InstructorId"];
                    result.AccountName = (string)dataReader["AccountName"];
                    result.Date = (DateTime)dataReader["Date"];
                    result.TypeContribution = (string)dataReader["TypeContribution"];
                    result.Duration = dataReader["Duration"].ToInt32OrDefault();
                    result.Point = dataReader["Point"].ToInt32OrDefault();
                    result.FinalPoint = dataReader["FinalPoint"].ToInt32OrDefault();
                    result.CreateBy = (int)dataReader["CreateBy"];
                    result.CreateDate = (DateTime)dataReader["CreateDate"];
                    result.ModifiedBy = (int)dataReader["ModifiedBy"];
                    result.ModifiedDate = (DateTime)dataReader["ModifiedDate"];
                    result.FeedBack = (int)dataReader["Feedback"];

                    return result;
                }
            }
            return null;
        }

        /// <summary>
        /// Search All Instructor by CourseId
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public Collection<CourseInstructor> CourseInstructorsByCourseId(int courseId) {
            var parameters = new Collection<SqlParameter> {
                                                              new SqlParameter("@CourseId", courseId)
                                                          };

            Collection<CourseInstructor> list = null;
            if (!string.IsNullOrEmpty(ProcedureConfiguration.SearchCourseInstructorByCourseId)) {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.SearchCourseInstructorByCourseId, parameters, ConnectionConfiguration.ConnectionString)) {
                    if (result != null) {
                        list = new Collection<CourseInstructor>();
                        while (result.Read()) {
                            list.Add(ParseToEntityCourseInstructor(result));
                        }
                    }
                }
            }
            return list;
        }


        public Collection<CourseInstructor> FetchAllByInstructorId(string instructorId) {
            var parameter = new Collection<SqlParameter> {
                                                             new SqlParameter("@InstructorId", instructorId)
                                                         };
            var list = new Collection<CourseInstructor>();
            using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.CourseInstructorFetchByInstructorId, parameter, ConnectionConfiguration.ConnectionString)) {
                if (result != null) {
                    while (result.Read()) {
                        list.Add(ParseToEntityCourseInstructor(result));
                    }
                }
            }
            return list;
        }

        #endregion
    }
}